﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IchiaTester.Utils;
using static IchiaTester.SerialPortConfigForm;
using static IchiaTester.MainForm;


namespace IchiaTester
{
    /// <summary>
    /// 串口配置项页面
    /// </summary>
    public partial class SerialPortItem : Form
    {
        /// <summary>
        /// 临时串口配置
        /// </summary>
        public SerialPortConfig tempSerialPortConfig { get; set; } = new SerialPortConfig();
        private SerialPortConfigForm form;

        private static List<string> availablePorts = new List<string>();
        public SerialPortItem(SerialPortConfigForm formSerialPortConfig)
        {
            this.form = formSerialPortConfig;
            InitializeComponent();
        }

        private void SerialPortItem_Load(object sender, EventArgs e)
        {
            try
            {
                cbPortName.Items.Clear();
                tbSyncFrame.Enabled = false;
                availablePorts = SerialPortHandler.GetAvailableComPorts();
                if (availablePorts.Count > 0)
                {
                    foreach (string port in availablePorts)
                    {
                        cbPortName.Items.Add(port);
                    }
                }
                else
                {
                    MessageBox.Show("No available serial ports found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                cbBaudRate.DropDownStyle = ComboBoxStyle.DropDownList;
                cbPortName.DropDownStyle = ComboBoxStyle.DropDownList;
                if (IsEdit)
                {
                    ComboBoxHandler.ConvertToSelectedIndex(cbPortName, tempSerialPortConfig.PortName);
                    ComboBoxHandler.ConvertToSelectedIndex(cbBaudRate, tempSerialPortConfig.BaudRate.ToString());
                    tbBufferSize.Text = tempSerialPortConfig.BufferSize.ToString();
                    CkbSyncOn.Checked = tempSerialPortConfig.SyncState;
                    CkbCRC.Checked = tempSerialPortConfig.CRCCheck.Equals("True") ? true : false;
                    tbSyncFrame.Enabled = tempSerialPortConfig.SyncState;
                    tbSyncFrame.Text = (tempSerialPortConfig.SyncState) ? tempSerialPortConfig.SyncFrame : "";
                }
                else
                {
                    //cbPortName.Items.AddRange(SerialPortConfigForm.AllAvailablePorts);
                    cbPortName.SelectedIndex = -1;
                    cbBaudRate.SelectedIndex = -1;
                    tbBufferSize.Text = "";
                    CkbSyncOn.Checked = false;
                    CkbCRC.Checked = false;
                    tbSyncFrame.Enabled = false;
                    tbSyncFrame.Text = "";
                }
            }
            catch (Exception ex)
            {
                ErrorLogAppend("Error occur while loading serial port item form", ex.Message + ex.StackTrace, SystemState.ErrorHandling);
            }
        }
        private void CkbSyncOn_CheckedChanged(object sender, EventArgs e)
        {
            tbSyncFrame.Enabled =CkbSyncOn.Checked;
            if (!CkbSyncOn.Checked)
            {
                tbSyncFrame.Text = "";
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                tempSerialPortConfig = new SerialPortConfig
                {
                    PortID = (IsEdit) ? SelectedPortID : form.AllSerialPortConfig.Count,
                    PortName = cbPortName.Text,
                    BaudRate = int.Parse(cbBaudRate.Text),
                    DataBits = 8,
                    Parity = "None",
                    StopBits = "One",
                    BufferSize = (String.IsNullOrEmpty(tbBufferSize.Text)) ? 0 : int.Parse(tbBufferSize.Text),
                    SyncState = CkbSyncOn.Checked,
                    SyncFrame = (CkbSyncOn.Checked) ? tbSyncFrame.Text : "",
                    CRCCheck = CkbCRC.Checked.ToString()
                };
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                ErrorLogAppend("Error occur while saving serial port item form", ex.Message + ex.StackTrace, SystemState.ErrorHandling);
            }

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            cbPortName.SelectedIndex = -1;
            cbBaudRate.SelectedIndex = -1;
            this.Close();
        }
    }
}
